<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//中文乱码问题用DW建立UTF-8文件
//header("Content-type: text/xml; charset=utf-8");

$arr[] = array('1','0','编号1');
$arr[] = array('2','0','编号2');
$arr[] = array('3','0','编号3');
$arr[] = array('4','1','编号4');
$arr[] = array('5','2','编号5');
$arr[] = array('6','3','编号6');
$arr[] = array('7','1','编号7');
$arr[] = array('8','7','编号8');
$arr[] = array('9','7','编号9');
$arr[] = array('10','7','编号10');

$code = json_encode($arr);
echo $code."<br />";
var_dump(json_decode($code));
echo "<br />";

/*
每级递归都有返回当某级递归结束会返至上级递归调用继续执行．
每级函数调用都有自己的私有变量．但函数代码并不会得到复制．
递归函数中必须包含可以终止递归调用的语句.类似于“嵌套函数”
*/

getExpandClass($arr,0);
//获取扩展分类
function getExpandClass(&$arr,$pid=0)
{
    foreach($arr as $key)
	{      //依次递归每个分类
           if($key[1]==$pid)
		   {    
                echo $key[2]."<br />"; 
				//自身id作为pid递归	
				getExpandClass($arr,$key[0]);   
			}
	}
}

echo "<br />";

getSonClass($arr,1);
//获取分类子类
function getSonClass(&$arr,$pid=0)
{
    foreach($arr as $key)
	{
           if($key[1]==$pid)
			{
				echo $key[2]."<br />";
			}
	}
}

?>
